# -*- coding: utf-8 -*-
import random
from manhua.myutils.User_Agent_list import USER_AGENT_LIST
from manhua.myutils.spider_xiciproxy_xpath import get_proxy_list


class UserAgentMiddleware(object):
    def __init__(self):
        self.user_agent_list = USER_AGENT_LIST

    def process_request(self, request, spider):
        user_agent = random.choice(self.user_agent_list)
        request.headers["User-Agent"] = user_agent


class ProxyMiddleware(object):
    def __init__(self):
        print '------------------------'
        print '------------------------'
        print '---------爬代理---------'
        print '------------------------'
        print '------------------------'
        self.proxy_list = get_proxy_list()


    def process_request(self, request, spider):
        print '------------------------'
        print '------------------------'
        print '--------代理池----------'
        print '------------------------'
        print '------------------------'
        print self.proxy_list
        proxy = random.choice(self.proxy_list)
        request.meta["proxy"] = "http://" + proxy
